package com.killer.auth.util;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.GetAliasesResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

@Component
@Slf4j
public class EsUtil {

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    /**
     * 获取某别名下的所有索引名
     * @param alias
     * @return
     */
    public String[] getAliasIndex(String alias) {
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest(alias);
        try {
            GetAliasesResponse response = restHighLevelClient.indices().getAlias(getAliasesRequest, RequestOptions.DEFAULT);
            Map<String, Set<AliasMetaData>> aliases = response.getAliases();
            return aliases.keySet().stream().toArray(String[]::new);
        } catch (IOException e) {
            log.error("查询别名异常");
        }
        return new String[]{};
    }

    public boolean changeAliasAfterReindex(String aliasName,  String newIndexName,String... oldIndexNames) {
        IndicesAliasesRequest.AliasActions addIndexAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD)
                .index(newIndexName).alias(aliasName);
        IndicesAliasesRequest indicesAliasesRequest=new IndicesAliasesRequest();
        indicesAliasesRequest.addAliasAction(addIndexAction);
        if (oldIndexNames != null && oldIndexNames.length > 0) {
            IndicesAliasesRequest.AliasActions removeAliasAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.REMOVE)
                    .indices(oldIndexNames).alias(aliasName);
            indicesAliasesRequest.addAliasAction(removeAliasAction);
        }
        try {
            AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
            return acknowledgedResponse.isAcknowledged();
        } catch (IOException e) {
            log.error("更改别名出错");
        }
        return false;
    }

    public boolean deleteIndex(String... oldIndexNames){
        //如果更换别名成功,则删除旧的索引
        DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest(oldIndexNames);
        try {
            AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
            return acknowledgedResponse.isAcknowledged();
        } catch (IOException e) {
            log.error("删除就索引失败");
        }
        return false;
    }

}
